$(function() {
    validMail();
    validPhone();
    ubTips();
});

function ubTips() { //悠币提示
    $('.ub').mouseenter(function() {
        var o = $('.tipsUb');
        o.show();
    }).mouseleave(function() {
        var o = $('.tipsUb');
        o.hide();
    })
};

var isSendEmail = false;
function validMail() {//邮箱验证流程
    $('#btnValMail').mouseenter(function() {
        var o = $(this);
        o.find('img').attr("src", "/Public/kanghui/images/mail_01.jpg");
    }).mouseleave(function() {
        var o = $(this);
        o.find('img').attr("src", "/Public/kanghui/images/mail_02.jpg");
    });

    $('#btnValMail').click(function() {
        var o = $(this);
        o.hide();
        $('#btnValMail2').show().find('img').attr("src", "/Public/kanghui/images/mail_01.jpg");
        $('.partvald').show();
    });

    $("#sendMail").click(function() {
        var email = $("#txtEmail").val();
        var isTrue = isTrueEmail(email, "sendEmailError");
        if (!isTrue) {
            return false;
        }

        $("#sendWait").css("display", "inline");
        $("#sendEmailError,#sendEmailSuccess,#sendMail").css("display", "none");
        $.ajax({
            type: 'post',
            url: "/SendEmailForVaildator",
            data: { "txtEmail": $("#txtEmail").val(), "cardNum": $("#txtCardNum").val(), "upPageURL": $("#txtUpPageURL").val(), "isRigisterValidate": $("#txtIsRigisterValidate").val(), "isValidateOrUpdate": $("#txtIsValidateOrUpdate").val() },
            success: function(data) {
            if (data == "threeError") {
                $("#sendEmailError").css("display", "inline").text("当日3次邮件验证发送机会已用完");
                    $("#sendEmailSuccess,#sendWait").css("display", "none");
                    $("#sendMail").css("display", "inline");
                } else if (data == "exists") {
                    $("#sendEmailError").css("display", "inline").text("邮箱已经存在");
                    $("#sendEmailSuccess,#sendWait").css("display", "none");
                    $("#sendMail").css("display", "inline");
                }
                else if (!isNaN(data)) {
                    $("#sendEmailError,#sendWait").css("display", "none");
                    $("#sendMail").text("重新发送验证邮件(" + data + "次)").attr("disabled", true);
                    regTimeMinusForEmail("sendMail", 4); //停留50苗
                    $("#sendEmailSuccess").css("display", "inline").html("验证邮件已发出，请注意查收。如果没收到，点击“重新发送验证邮件”按钮。<br/>");
                }
                else if (data != "") {
                    $("#sendEmailError").css("display", "inline").text(data);
                    $("#sendEmailSuccess,#sendWait").css("display", "none");
                    $("#sendMail").css("display", "inline");
                }
            },
            error: function() {
            }
        });
    });
};

function updateMobilePV() {
    if (isSendMsg) {
        alert("正在发送短信验证码，请在60秒倒计时结束后再修改手机");
        return false;
    } else {
        $('#popwindow').css('display', 'block');
    }
}

function updateEmailPV() {
    if (isSendEmail) {
        alert("正在发送验证邮件，请在稍后再修改邮箱");
        return false;
    } else {
        $('#popwindow').css('display', 'block');
    }
}

var isSendMsg = false;
function validPhone() {//手机验证流程
    $('#btnValPhone').mouseenter(function() {
        var o = $(this);
        o.find('img').attr("src", "/Public/kanghui/images/phone_01.jpg");
    }).mouseleave(function() {
        var o = $(this);
        o.find('img').attr("src", "/Public/kanghui/images/phone_02.jpg");
    });

    $('#btnValPhone').click(function() {
        var o = $(this);
        o.hide();
        $('#btnValPhone2').show().find('img').attr("src", "/Public/kanghui/images/phone_01.jpg");
        $('.partvald').show();
    });
    $("#sendPhone").click(function () {
        var phone = $("#txtPhone").val();
        var isTrue = isTrueMobile(phone, "phoneError");
        if (!isTrue) {
            return false;
        }
        isTrue = isTrueCheckCode($("#txtCheckCode").val(), "phoneError");
        if (!isTrue) {
            return false;
        }
        $("#sendWait").css("display", "inline");
        $("#phoneError,#mobileSendSuccess,#sendPhone").css("display", "none");
        var txtCheckCode = $("#txtCheckCode").val();
        if ($("#checkCodeForSend").css("display") == "none")
            txtCheckCode = "none";
        $.ajax({
            type: "post",
            url: "/SendMobileCheckCode",
            data: { "txtPhone": phone, "type": "validatePhone", "txtCheckCode": txtCheckCode },
            success: function (data) {
                $("#checkCodeForSend").hide();
                $("#txtCheckCode").val("none");
                if (data != "-4") {
                    $("#txtCheckCode").val("");
                    $("#imgCheckCode").click();
                }
                if (data == "-4") {
                    $("#phoneError").css("display", "inline").html("<span>填写的数字和图片中的不符</span>");
                    $("#sendWait,#mobileSendSuccess").css("display", "none");
                    $("#sendPhone").css("display", "inline");
                    $("#checkCodeForSend").show();
                    $("#txtCheckCode").val("");
                }
                else if (data == "-2") {
                    $("#phoneError").css("display", "inline").html("<span>手机号已经存在</span>");
                    $("#sendWait,#mobileSendSuccess").css("display", "none");
                    $("#sendPhone").css("display", "inline");
                }
                else if (data == "-1") {
                    $("#phoneError").css("display", "inline").html("<span>当日3次手机验证机会已经用完</span>");
                    $("#sendWait,#mobileSendSuccess").css("display", "none");
                    $("#sendPhone").css("display", "inline");
                }
                else {
                    $("#phoneError").css("display", "none")
                    if (data == "false") {
                        $("#sendPhone").attr("disabled", true).css("display", "inline");
                    } else {
                        $("#sendPhone").attr("disabled", true).css("display", "inline").text("重新发送短信验证码(" + data + "次)");
                    }
                    $("#sendWait").css("display", "none");
                    regTimeMinus("sendPhone", 60); //停留60秒
                    $("#mobileSendSuccess").css("display", "inline").html("验证码已发出，请注意查收短信。如果没收到，<span id='showTime'></span>点击“重新发送短信验证码”按钮。<br/>");
                }
            },
            error: function () { }
        });
    });
};

//重新计算发送验证码的时间
function regTimeMinus(btn, t) {
    if (t > 1) {
        isSendMsg = true;
        t = t - 1;
        $("#showTime").html("请<i class='timer'>" + (t < 10 ? "0" + t : "" + t) + "</i>秒后");
        //执行完后，再次执行
        timeCode = window.setTimeout("regTimeMinus('" + btn + "', " + t + ")", 1000);
    }
    else if (t <= 1) {
    $('#' + btn).attr('disabled', false);
    isSendMsg = false;
        $("#showTime").html("现在可以");
    }
}

function regTimeMinusForEmail(btn, t) {
    if (t > 1) {
        isSendEmail = true;
        t = t - 1;
        $("#showTime").html("<img style='margin-left:25px' src='/Public/kanghui/images/onLoad.gif'><span class='gray' style='margin-left:5px'>邮件发送中...</span>");
        //执行完后，再次执行
        timeCode = window.setTimeout("regTimeMinusForEmail('" + btn + "', " + t + ")", 1000);
    }
    else if (t <= 1) {
    $('#' + btn).css("display","inline").attr('disabled', false);
    isSendEmail = false;
        $("#showTime").html("");
    }
}

//验证手机
function mySumbit() {
    var mobile = $("#txtPhone").val();
    var isTrue = isTrueMobile(mobile, "phoneError");
    if (!isTrue) {
        return false;
    }
    var checkCode = $("#txtPhoneCode").val();
    isTrue = isTrueCheckCode(checkCode, "checkCodeError");
    if (!isTrue) {
        return false;
    }
    $("#sumbitWaitDiv").css("display", "inline");
    $("#submitDiv").css("display", "none");
    $.ajax({
        type: 'post',
        url: "/SaveValidatePhone",
        data: { "txtPhone": $("#txtPhone").val(), "txtPhoneCode": $("#txtPhoneCode").val() },
        success: function(data) {
        $("#sumbitWaitDiv").css("display", "none");
            $("#submitDiv").css("display", "inline");
            if (data.indexOf("验证码") >= 0) {
                $("#checkCodeError").css("display", "inline").text(data);
                return false;
            } else if (data.indexOf("手机") >= 0) {
                $("#phoneErroralert").css("display", "inline").text(data);
                return false;
            } else if (data != "") {
                alert(data);
                return false;
            } else {
                if ($("#txtIsRigisterValidate").val().toLowerCase() != "false") {
                    location.href = "/PhoneSuccess?cardNum=" + $("#txtCardNum").val() + "&UpPageURL=" + $("#txtUpPageURL").val() + "&isValidateOrUpdate=" + $("#txtIsValidateOrUpdate").val();
                } else {
                    location.href = "/manage/personal-info/PhoneSuccess?isValidateOrUpdate=" + $("#txtIsValidateOrUpdate").val();
                }
            }
        },
        error: function() { return false; }
    });
    return true;
}

//验证修改手机
function mySubmit2() {
    var mobile = $("#txtUpdatePhone").val();
    var isTrue = isTrueMobile(mobile, "txtUpdatePhoneError");
    if (!isTrue) {
        return false;
    }
    var checkCode = $("#txtUpdatePhoneCode").val();
    isTrue = isTrueCheckCode(checkCode, "updatePhoneCodeError");
    if (!isTrue) {
        return false;
    }
    $.ajax({
        type: 'post',
        url: "/UpdateMobileForVaildator",
        data: { "txtUpdatePhone": $.trim($("#txtUpdatePhone").val()), "txtUpdatePhoneCode": $.trim($("#txtUpdatePhoneCode").val()) },
        success: function(data) {
            if (data == "") {
                $("#popwindow").css("display", "none");
                $("#spanPhone").text(mobile)
                $("#txtPhone").val(mobile);
                $("#sendPhone").attr("disabled", false).text("发送短信验证码");
                $("#showPhone").text(mobile);
            }
            else if (data == "existsMobile")
                $("#txtUpdatePhoneError").css("display", "inline").html("该手机号码已存在，点此<a href=\"#\">登录</a>");
            else if (data.indexOf("验证码") >= 0)
                $("#updatePhoneCodeError").css("display", "inline").text("验证码错误");
            else
                alert(data);
        },
        error: function() {
        }
    });
}

//验证修改邮箱
function mySubmit3() {
    var email = $("#txtUpdateEmail").val();
    var isTrue = isTrueEmail(email, "txtUpdateEmailError");
    if (!isTrue) {
        return false;
    }
    var checkCode = $("#txtUpdateEmailCode").val();
    isTrue = isTrueCheckCode(checkCode, "updateEmailCodeError");
    if (!isTrue) {
        return false;
    }
    $.ajax({
        url: "/UpdateEmailForVaildator",
        data: { "txtUpdateEmail": $.trim($("#txtUpdateEmail").val()), "txtUpdateEmailCode": $.trim($("#txtUpdateEmailCode").val()) },
        success: function(data) {
            if (data == "") {
                $("#popwindow").css("display", "none");
                $("#spanEmail").text(email)
                $("#txtEmail").val(email);
                $("#sendMail").attr("disabled", false).text("发送验证邮件"); 
                $("#showEmail").text(email);
                $("#")
            }
            else if (data == "existsEmail")
                $("#txtUpdateEmailError").css("display", "inline").html("该邮箱已存在，点此<a href=\"#\">登录</a>");
            else if (data.indexOf("验证码") >= 0)
                $("#updateEmailCodeError").css("display", "inline").text("验证码错误");
            else
                alert(data);
        },
        error: function() {
        }
    });
}


function isTrueEmail(email, obj) {
    if ($.trim(email) == "") {
        $("#" + obj).css("display", "inline").html("邮箱不能为空");
        return false;
    }
    if (!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(email)) {
        $("#" + obj).css("display", "inline").html("邮箱格式错误");
        return false;
    }
    $("#" + obj).css("display", "none");
    return true;
}

function isTrueMobile(mobile, obj) {
    if ($.trim(mobile) == "") {
        $("#" + obj).css("display", "inline").html("手机不能为空");
        return false;
    }
    if (mobile.length != 11) {
        $("#" + obj).css("display", "inline").html("手机格式错误");
        return false;
    }
    if (!/^13[0-9]{9}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}$/.test(mobile)) {
        $("#" + obj).css("display", "inline").html("手机格式错误");
        return false;
    }
    $("#" + obj).css("display", "none");
    return true;
}

function isTrueCheckCode(checkCode, obj) {
    if ($("#checkCodeForSend").css("display") != "none") {
        if ($.trim(checkCode) == "") {
            $("#" + obj).css("display", "inline").text((obj == "phoneError" ? "请输入图片中的数字" : "验证码不能为空"));
            return false;
        }
        if (checkCode.length != 4 || !/^[0-9]\d*|0$/.test(checkCode)) {
            $("#" + obj).css("display", "inline").text((obj == "phoneError" ? "图片中为四位数字" : "验证码为4位数字"));
            return false;
        }
    }
    $("#" + obj).css("display", "none");
    return true;
}